#include "tcp_link.hpp"
#include <string.h>

namespace  rocket
{
tcp_link::tcp_link(int client_fd)
:_client_fd(client_fd)
,_local_address(local_address())
,_peer_address(peer_address())
,_io(_client_fd.get_socket())
{

}
tcp_link::~tcp_link()
{

}
sockaddr_in tcp_link::local_address()
{
    sockaddr_in temp;
    memset(&temp, 0, sizeof(temp));
    socklen_t len=sizeof(temp);
    getsockname(_client_fd.get_socket(), (sockaddr *)&temp,&len);
    return temp;
}

sockaddr_in tcp_link::peer_address()
{
    sockaddr_in temp;
    memset(&temp, 0, sizeof(temp));
    socklen_t len=sizeof(temp);
    getpeername(_client_fd.get_socket(), (sockaddr *)&temp,&len);
    return temp;
}
int tcp_link::receive(char *buff,int max_length)
{
    int ret=_io.read_line(buff, max_length);
    return ret;
}

int tcp_link::send(char *buff,int length)
{
    int ret=_io.write_n(buff, length);
    return ret;
}

int tcp_link::read_packet(packet &pac)
{
    int ret=_io.read_packet(pac);
    return ret;
}

int tcp_link::send_tlv(tlv data)
{
    int ret=send(data.get_tlv_head(), data.get_length()+8);
    return ret;
}


}


